package com.atguigu.controller;

import com.atguigu.domain.EduCourse;
import com.atguigu.domain.EduTeacher;
import com.atguigu.domain.vo.CourseInfoVo;
import com.atguigu.domain.vo.CoursePublishVo;
import com.atguigu.domain.vo.CourseQuery;
import com.atguigu.service.EduCourseDescriptionService;
import com.atguigu.service.EduCourseService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;


    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id = eduCourseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",id);
    }

    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("coursePublish",coursePublishVo);
    }

    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //TODO 条件查询  带分页 待完善
    @PostMapping("{page}/{limit}")
    public R getCourseList(@PathVariable long page, @PathVariable long limit,
                           @RequestBody CourseQuery courseQuery){
        Page<EduCourse> coursePage = new Page<EduCourse>(page, limit);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<EduCourse>();

        String title = courseQuery.getName();
        String status = courseQuery.getStatus();

        if(!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }

        queryWrapper.orderByDesc("gmt_create");

        eduCourseService.page(coursePage,queryWrapper);
        Long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();
        Map map = new HashMap();
        map.put("total",total);
        map.put("list",records);
        return R.ok().data(map);
    }

    @DeleteMapping("{id}")
    public R deleteCourse(@PathVariable String id){
        eduCourseService.removeCourse(id);
        return R.ok();
    }


}
